#include "form.h"
#include "pushbutton.h"
#include "MainWindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSize>
#include <QApplication>
#include <QLabel>
#include <QBrush>
#include <QPalette>



Form::Form(QWidget *parent) :
    QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    //this->setAttribute(Qt::WA_TranslucentBackground);

    mpMainVbl = new QVBoxLayout;
    mpTitleHbl = new QHBoxLayout;
    mpMinPbtn = new PushButton;
    mpMaxPbtn = new PushButton;
    mpClosePbtn = new PushButton;
    mpTitleLbl = new QLabel(tr("LPlayer V2.0 Beta"));

    mpMainMwd = new MainWindow;

    setPushButton(mpMinPbtn);
    setPushButton(mpMaxPbtn);
    setPushButton(mpClosePbtn);

    //mpTitleLbl->setStyleSheet("color:white;");
    mpTitleHbl->addWidget(mpTitleLbl);
    mpTitleHbl->addStretch();
    mpTitleHbl->addWidget(mpMinPbtn);
    mpTitleHbl->addWidget(mpMaxPbtn);
    mpTitleHbl->addWidget(mpClosePbtn);

    mpMainVbl->addLayout(mpTitleHbl);
    mpMainVbl->addWidget(mpMainMwd);

    setLayout(mpMainVbl);
    //ui->gridLayout->setContentsMargins(SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH, SHADOW_WIDTH);
    this->setGeometry(0,0,800,450);

     //注意要先调用setAutoFillBackground
    setAutoFillBackground(true);
    QPalette palette;
    //设置QTextEdit文字颜色
    //palette.setBrush(QPalette::Active, QPalette::Text, QBrush(Qt::yellow));
    //设置QTextEdit背景色
    palette.setColor(QPalette::Active, QPalette::Window, Qt::black);
    setPalette(palette);
    palette.setColor(QPalette::Inactive, QPalette::Window, Qt::black);
    setPalette(palette);
    palette.setColor(QPalette::Active, QPalette::WindowText, Qt::white);
    setPalette(palette);
    palette.setColor(QPalette::Inactive, QPalette::WindowText, Qt::white);
    setPalette(palette);
    mpMinPbtn->loadPixmap(":/ButtonMin.png");
    mpMaxPbtn->loadPixmap(":/ButtonMax.png");
    mpClosePbtn->loadPixmap(":/ButtonClose.png");

    this->setMouseTracking(true);
    //设置在不按鼠标的情况下也触发鼠标移动事件，注意QMainWindow的情况:centralWidget()->setMouseTracking(true);
    isLeftPressed=false;
    curPos=0;//标记鼠标左击时的位置
    //this->setMinimumSize(400,300);//设置最小尺寸
    QCursor cursor;
    cursor.setShape(Qt::ArrowCursor);//设置鼠标为箭头形状
    // ui->pushButton->setCursor(cursor);//当放在按钮上时，为箭头
    // cursor.setShape(Qt::OpenHandCursor);
    QWidget::setCursor(cursor);//当放在主窗口上时，为手形
    qDebug()<<"h="<<this->height();
    setWindowFlags(Qt::FramelessWindowHint);//设置主窗口无边框
    qDebug()<<this->minimumHeight();

    connect(mpClosePbtn,SIGNAL(clicked(bool)),SLOT(on_closePbtn_clicked()));
    connect(mpMaxPbtn,SIGNAL(clicked(bool)),SLOT(on_maxPbtn_clicked()));
}

Form::~Form()
{

}

void Form::on_closePbtn_clicked()
{
    exit(0);
}

void Form::on_maxPbtn_clicked()
{
    if(this->windowState() != Qt::WindowMaximized)
        this->setWindowState(Qt::WindowMaximized);
    else
        this->setWindowState(Qt::WindowNoState);
}

void Form::on_pushButton_3_clicked()
{
    this->setWindowState(Qt::WindowMinimized);
}

MainWindow* Form::getMainWindowAddr()
{
    return mpMainMwd;
}

void Form::setPushButton(PushButton *pbtn)
{
    QSize size;
    size.setHeight(20);
    size.setWidth(20);
    pbtn->setMinimumSize(size);
    pbtn->setMaximumSize(size);
}

void Form::mousePressEvent(QMouseEvent *event)//鼠标按下事件
{
  if(event->button()==Qt::LeftButton)
  {
    this->isLeftPressed=true;
    QPoint temp=event->globalPos();
    pLast=temp;
    curPos=countFlag(event->pos(),countRow(event->pos()));
    event->ignore();
  }
}

void Form::mouseReleaseEvent(QMouseEvent *event)//鼠标释放事件
{
  if(isLeftPressed)
    isLeftPressed=false;
  QApplication::restoreOverrideCursor();//恢复鼠标指针性状
  event->ignore();
}

void Form::mouseDoubleClickEvent(QMouseEvent *event)//鼠标双击 全屏
{
  if(event->button()==Qt::LeftButton)
  {
    if(windowState()!=Qt::WindowFullScreen)
      setWindowState(Qt::WindowFullScreen);
    else setWindowState(Qt::WindowNoState);//恢复正常模式
  }
  event->ignore();
}

void Form::mouseMoveEvent(QMouseEvent *event)//鼠标移动事件
{

  int poss=countFlag(event->pos(),countRow(event->pos()));
  setCursorType(poss);
  if(isLeftPressed)//是否左击
  {
    QPoint ptemp=event->globalPos();
    ptemp=ptemp-pLast;
    if(curPos==22)//移动窗口
    {
      ptemp=ptemp+pos();
      move(ptemp);
    }
    else
    {
      QRect wid=geometry();

      switch(curPos)//改变窗口的大小
      {

      case 11:wid.setTopLeft(wid.topLeft()+ptemp);break;//左上角
      case 13:wid.setTopRight(wid.topRight()+ptemp);break;//右上角
      case 31:wid.setBottomLeft(wid.bottomLeft()+ptemp);break;//左下角
      case 33:wid.setBottomRight(wid.bottomRight()+ptemp);break;//右下角
      case 12:wid.setTop(wid.top()+ptemp.y());break;//中上角
      case 21:wid.setLeft(wid.left()+ptemp.x());break;//中左角
      case 23:wid.setRight(wid.right()+ptemp.x());break;//中右角
      case 32:wid.setBottom(wid.bottom()+ptemp.y());break;//中下角
      }
      setGeometry(wid);

    }


    pLast=event->globalPos();//更新位置
  }
  event->ignore();
}



int Form::countFlag(QPoint p,int row)//计算鼠标在哪一列和哪一行
{
  if(p.y()<MARGIN)
    return 10+row;
  else if(p.y()>this->height()-MARGIN)
    return 30+row;
  else
    return 20+row;
}

void Form::setCursorType(int flag)//根据鼠标所在位置改变鼠标指针形状
{
  Qt::CursorShape cursor;
  switch(flag)
  {
  case 11:
  case 33:
    cursor=Qt::SizeFDiagCursor;break;
  case 13:
  case 31:
    cursor=Qt::SizeBDiagCursor;break;
  case 21:
  case 23:
    cursor=Qt::SizeHorCursor;break;
  case 12:
  case 32:
    cursor=Qt::SizeVerCursor;break;
  case 22:
    cursor=Qt::OpenHandCursor;break;
  default:
     //  QApplication::restoreOverrideCursor();//恢复鼠标指针性状
     break;

  }
  setCursor(cursor);
}

int Form::countRow(QPoint p)//计算在哪一列
{
  return (p.x()<MARGIN)?1:(p.x()>(this->width()-MARGIN)?3:2);
}


